package com.ymatou.iapi.productmanager.testcase;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.ModifyProductPicCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;

public class Ts_ModifyProductPic {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App-图片修改");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new ModifyProductPicCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ModifyProductPic_001() {
		Logger.start(true, "App-图片修改");
		try{
			List<String> picUrls = new ArrayList<String>();
			picUrls.add("http://p11.img.ymatou.com/G01/M01/00/E5/rBBk0VXIqXOAP8WIAAFuo51Y5lA11_o.jpg");
			picUrls.add("http://p22.img.ymatou.com/G01/M02/00/E5/rBBk0VXIqXOAP8WIAAFuo51Y5lA22_o.jpg");
			
			//1 add new product
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			//2 modify pic
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ProductId", productId);
			updateMap.put("ProductImgUrls", new JSONArray(picUrls));
			
			JSONObject modifyPicBean = new ModifyProductPicCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(200, modifyPicBean.getInt("Code"), "响应报文-返回码");
			
			
			SellerProductCallService.checkSQLAndMongo_ModifyPicture(productId,picUrls);
			
			NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ModifyProductPic_002() {
		Logger.start(true, "商品不存在");
		try{
			List<String> picUrls = new ArrayList<String>();
			picUrls.add("http://p11.img.ymatou.com/G01/M01/00/E5/rBBk0VXIqXOAP8WIAAFuo51Y5lA11_o.jpg");
			picUrls.add("http://p22.img.ymatou.com/G01/M02/00/E5/rBBk0VXIqXOAP8WIAAFuo51Y5lA22_o.jpg");
			
			
			//2 modify pic
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ProductId", "123123213213");
			updateMap.put("ProductImgUrls", new JSONArray(picUrls));
			
			JSONObject modifyPicBean = new ModifyProductPicCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(201, modifyPicBean.getInt("Code"), "响应报文-返回码");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ModifyProductPic_003() {
		Logger.start(true, "图片不存在");
		try{
			List<String> picUrls = new ArrayList<String>();
			picUrls.add("");
			
			//1 add new product
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			//2 modify pic
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ProductId", productId);
			updateMap.put("ProductImgUrls", new JSONArray(picUrls));
			
			JSONObject modifyPicBean = new ModifyProductPicCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(201, modifyPicBean.getInt("Code"), "响应报文-返回码");
			
			
			
			updateMap.put("ProductImgUrls", new JSONArray(new ArrayList<>()));
			JSONObject modifyPicBean1 = new ModifyProductPicCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(201, modifyPicBean1.getInt("Code"), "响应报文-返回码");
			
			
			updateMap.put("ProductImgUrls", new JSONArray());
			JSONObject modifyPicBean2 = new ModifyProductPicCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(201, modifyPicBean2.getInt("Code"), "响应报文-返回码");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	

}
